package com.zgc.dao.impl;

import java.util.List;

import javax.annotation.Resource;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import org.springframework.stereotype.Repository;

import com.zgc.dao.ISourceItemDao;
import com.zgc.domain.SourceItem;
import com.zgc.domain.User;
import com.zgc.utils.ZGCContext;
@Repository
public class SourceItemDaoImpl extends HibernateDaoSupport implements ISourceItemDao {
	@Resource
	public void setSF(SessionFactory sessionFactory){
		super.setSessionFactory(sessionFactory);
	}

	public List<SourceItem> queryAllItem() {
		User loginUser = ZGCContext.getLoginUser();
		if (loginUser != null) {
			Integer uid = loginUser.getUid();
			List list = this.getHibernateTemplate().find(" from SourceItem where uid = ? ",uid);
			return list;
		}else {
			return null;
		}
	}

	public List<SourceItem> queryItemByStatus(int status) {
			List list = this.getHibernateTemplate().find(" from SourceItem where status = ? ",status);
			return list;
		
	}

	public SourceItem qryItemById(int itemId) {
		SourceItem sourceItem = this.getHibernateTemplate().get(SourceItem.class, itemId);
		return sourceItem;
	}

	public void update(SourceItem sourceItem) {
		this.getHibernateTemplate().update(sourceItem);
	}

	public List<SourceItem> queryItemByStatus(int status, Integer uid) {
		List list = this.getHibernateTemplate().find(" from SourceItem where status = ? and uid = ? ",status,uid);
		return list;
	}

	public void insertSourceItem(SourceItem sourceItem) {
		this.getHibernateTemplate().save(sourceItem);
	}

	@Override
	public int getAllRowCount(String hql) {
		return this.getHibernateTemplate().find(hql).size();
	}

	public List<SourceItem> queryForPage(final String hql, final int offset, final int length) {    
		Session session = this.getSession();    
		Query q = session.createQuery(hql);    
		q.setFirstResult(offset);    
		q.setMaxResults(length);    
		List<SourceItem> list = q.list();    
		System.out.println(list.size());    
//		session.close();    
		return list;    
		}    
}
